import Vue from 'vue'
import VueRouter from 'vue-router'
import { getCookit } from '../utils'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: '/my',
        name: 'My',
        component: () => import('../views/My.vue')
      },
      {
        path: '/main',
        name: 'Main',
        component: () => import('../views/Main.vue')
      },
      {
        path: '/detail/:id',
        name: 'Detail',
        component: () => import('../views/Detail.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/edit',
    name: "Edit",
    component: () => import('../views/Edit.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {

  if (!getCookit() && to.path !== '/login') {
    next('/login')
  }

  next()

})

export default router
